<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog" title="商品折扣" width="900px">
    <el-form ref="form" :model="form" :inline="true" size="small" label-width="140px">
      <el-form-item label="折扣名称">
        <el-input v-model="form.title" style="width: 500px;" />
      </el-form-item>
      <el-form-item label="折扣简介">
        <el-input v-model="form.info" style="width: 500px;" rows="5" type="textarea" />
      </el-form-item>
      <el-form-item label="开始时间">
        <template>
          <el-date-picker
            v-model="form.startTimeDate"
            type="datetime"
            placeholder="选择日期时间"
          />
        </template>
      </el-form-item>
      <el-form-item label="结束时间">
        <template>
          <el-date-picker
            v-model="form.endTimeDate"
            type="datetime"
            placeholder="选择日期时间"
          />
        </template>
      </el-form-item>
      <div v-for="(item, index) in levels" :key="index" style="display: flex;">
        <el-form-item label="会员等级">
          <el-input v-model="item.name" disabled/>
        </el-form-item>
        <el-form-item label="折扣率(%)">
          <el-input v-model="item.discount" />
        </el-form-item>
        <el-form-item label="折扣价">
          <el-input :value="(form.price*item.discount/100).toFixed(2)"/>
        </el-form-item>
      </div>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit, search } from '@/api/PmStoreDiscount'
import initData from '@/mixins/crud'
import { getLevels } from  '@/api/PmSystemUserLevel'

export default {
  components: { },
  props: {
    isLevel: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: {
        id: '',
        productId: '',
        price: 0,
        grade: '',
        discount: '',
        title: '',
        startTimeDate: '',
        endTimeDate: '',
        status: 1,
        info: '',
        sort: 0,
        isDel: 0,
        addTime: '',
        jsonStr: ''
      },
      levels: [],
      levelObj: {}
    }
  },
  mounted(){

  },
  watch: {
      form : function(newVal){
        let that = this;
        if(newVal.productId == ""){
          return ;
        }
        search(newVal.productId).then(res => {
          if(res.jsonStr != null){
            that.form.title = res.title;
            that.form.info = res.info;
            that.form.startTimeDate = res.startTimeDate;
            that.form.endTimeDate = res.endTimeDate;
            that.levels = JSON.parse(res.jsonStr);
          }else {
            getLevels({}).then(({ content })=> {
              for (let item of content) {
                let levelObj = {};
                levelObj.id = item.id;
                levelObj.name = item.name;
                levelObj.grade = item.grade;
                levelObj.discount = '';
                that.levels.push(levelObj);
              }
            })
          }
        })

      }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isLevel) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      let startTime = this.form.startTimeDate;
      let endTime = this.form.endTimeDate;

      this.form.startTimeDate = startTime == "" ? new Date() : startTime;
      this.form.endTimeDate = endTime == "" ? new Date("2029-01-01 00:00:00") : endTime;

      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
      })
    },
    doEdit() {
      let startTime = this.form.startTimeDate;
      let endTime = this.form.endTimeDate;

      this.form.startTimeDate = startTime == "" ? new Date() : startTime;
      this.form.endTimeDate = endTime == "" ? new Date("2029-01-01 00:00:00") : endTime;

      let jsonStr = JSON.stringify(this.levels);
      let params = Object.assign(this.form, {jsonStr: jsonStr})
      edit(params).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields();
      this.levels = [];
      this.form = {
        id: '',
        productId: '',
        price: 0,
        grade: '',
        discount: '',
        title: '',
        startTimeDate: '',
        endTimeDate: '',
        status: 1,
        info: '',
        sort: 0,
        isDel: 0,
        addTime: '',
        jsonStr: ''
      }
    }
  }
}
</script>

<style scoped>
  /deep/ .el-form-item--small .el-form-item__content {
    width: 48%;
  }
</style>
